/*===========================================================================================================================
	Wolf Prop Protection
		By Goluch
			DO NOT UPLOAD TO GARRYSMOD.ORG
			
			
	Section: Hud information
===========================================================================================================================*/
local WPP = WPP --Aparently its faster.
WPP.IconOn = surface.GetTextureID("gui/silkicons/check_on")
WPP.IconOff = surface.GetTextureID("gui/silkicons/check_off")
WPP.HUDModes = {}
WPP.HUDCvar = CreateClientConVar("WPP_HUDMode","Side Box",true);

/*===========================================================================================================================		
	Name: WPP.HudIndex
	Purpose: Gets local players HUD index
	Author: Goluch
===========================================================================================================================*/
function WPP:HudIndex()
	local Weapon = LocalPlayer():GetActiveWeapon()
	if ( !Weapon ) or ( !Weapon:IsValid() ) then
		return;
	end
	
	Weapon = Weapon:GetClass();
	
	local Index = WPP:GetFromWeapon(Weapon,LocalPlayer());
	
	if ( !Index ) then
		return;
	end
	
	return Index;
end

/*===========================================================================================================================		
	Name: WPP.HudString
	Purpose: Gets local players HUD index
	Author: Goluch
===========================================================================================================================*/
function WPP:HudString(Entity)
	if ( IsValid(Entity) ) then
		if ( WPP:IsOwned(Entity) ) then
			local Owner = WPP:GetOwner(Entity);
			local OwnerName = "Disconected";
			
			if ( Owner ) and ( Owner:IsValid() ) then
				OwnerName = Owner:Nick();
			end
			
			return OwnerName
		end
	end
end

/*===========================================================================================================================		
	Name: WPP.HUDModes.Off
	Purpose: disable hud.
	Author: Goluch
===========================================================================================================================*/
function WPP.HUDModes.Off()
end

/*===========================================================================================================================		
	Name: WPP.HUDModes.SideBox
	Purpose: Draws a SideBox.
	Author: Goluch
===========================================================================================================================*/
WPP.HUDModes["Side Box"] = function()
	local Player = LocalPlayer();
	local Entity = Player:GetEyeTrace().Entity;
	local Name, Index = WPP:HudString(Entity), WPP:HudIndex();
	if ( !Name ) or ( !Index ) then
		return;
	end
	
	surface.SetFont("Default");
	local Width, Height = surface.GetTextSize(Name);
	local Border = 4;
	
	Height = Height + (Border * 2);
	Width = Width + Height + Border;
	local X, Y = ScrW() - Width - 10, 10;
			
	local DrawColor = Color(255,0,0,255);
	local Icon = WPP.IconOff;
	
	if ( WPP:ValidateAction(Index,Entity,Player) ) then
		DrawColor = Color(0,255,0,255);
		Icon = WPP.IconOn;	
	end
	
	draw.RoundedBox(4, X, Y, Width, Height, Color(0, 0, 0, 150));
	draw.SimpleText(Name, "Default", X + Height, Y + (Height / 2), DrawColor, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER);
	
	surface.SetDrawColor( 255, 255, 255, 255 );
	surface.SetTexture( Icon );
	surface.DrawTexturedRect( X + Border, Y + Border , Height - (Border * 2) , Height - (Border * 2) );
end

/*===========================================================================================================================		
	Name: WPP.HUDModes["Center Box"]
	Purpose: Draws a CenterBox.
	Author: Goluch
===========================================================================================================================*/
WPP.HUDModes["Center Box"] = function(Entity)
	local Player = LocalPlayer();
	local Entity = Entity || Player:GetEyeTrace().Entity; //We will use this function for ShowAll Later
	local Name, Index = WPP:HudString(Entity), WPP:HudIndex();
	if ( !Name ) or ( !Index ) then
		return;
	end
	
	surface.SetFont("Default");
	local Width, Height = surface.GetTextSize(Name);
	
	local Position = Entity:LocalToWorld( Entity:OBBCenter() ):ToScreen();
	local X, Y = Position.x , Position.y;
	
	local DrawColor = Color(255,0,0,255);
	local Icon = WPP.IconOff;
	
	if ( WPP:ValidateAction(Index,Entity,Player) ) then
		DrawColor = Color(0,255,0,255);
		Icon = WPP.IconOn;	
	end
	
	surface.SetTexture( Icon );
	surface.SetDrawColor( DrawColor.r, DrawColor.g, DrawColor.b, 150 );
	surface.DrawTexturedRect( X - 5, Y - 5, 10, 10 );
	
	surface.SetDrawColor( DrawColor.r, DrawColor.g, DrawColor.b, 150 );
	surface.DrawOutlinedRect( X - 5, Y - 5, 10, 10 );
	
	draw.SimpleText(Name, "Default", X , Y - 15 - (Height/2) , DrawColor, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER);
end

/*===========================================================================================================================		
	Name: WPP.HUDModes["Center Box"]All
	Purpose: Draws a CenterBox for all entitys.
	Author: Goluch
===========================================================================================================================*/
WPP.HUDModes["Show All"] = function(Entity)
	local Player = LocalPlayer();
	local Entity = Player:GetEyeTrace().Entity;
	
	if ( IsValid(Entity) ) and ( WPP:IsOwner(Entity) ) then
		WPP.HUDModes["Center Box"](Entity);
		return;
	end
	
	for k,Entity in pairs( ents.GetAll() ) do
		WPP.HUDModes["Center Box"](Entity);
	end
end

/*===========================================================================================================================		
	Name: WPP.HUDModes["Center Box"]Close
	Purpose: Draws a CenterBox for all entitys.
	Author: Goluch
===========================================================================================================================*/
WPP.HUDModes["Show All Close Range"] = function(Entity)
	local Player = LocalPlayer();
	local Entity = Player:GetEyeTrace().Entity;
	
	if ( IsValid(Entity) ) and ( WPP:IsOwner(Entity) ) then
		WPP.HUDModes["Center Box"](Entity);
		return;
	end
	
	local Pos = Player:GetPos();
	for k,Entity in pairs( ents.GetAll() ) do
		local Distance = Pos:Distance( Entity:GetPos() ) * 0.0254; --to meters
		if ( Distance <= 15 ) then
			WPP.HUDModes["Center Box"](Entity);
		end
	end
end

/*===========================================================================================================================		
	Hook: WPP.HUDPaint
	Purpose: Draws the hud.
	Author: Goluch
===========================================================================================================================*/
hook.Add("HUDPaint","WPP-HUDPaint",function()
	WPP:DrawHint();
	
	local Mode = WPP.HUDCvar:GetString() || "";
	if ( !WPP.HUDModes[Mode] ) then
		Mode = "Side Box";
	end
	
	local Ok, Why = pcall(WPP.HUDModes[Mode]);
	if ( !Ok ) then
		print("[WPP] Hud Error: " .. Why);
	end
end)

/*===========================================================================================================================		
	Section: Hints
===========================================================================================================================*/
WPP.Hint = "";
WPP.Hint_Color = Color(255,255,255);
WPP.Hint_Time = 0;

/*===========================================================================================================================		
	Name: WPP.DrawHint
	Purpose: Draws a WPP hint.
	Author: Goluch
===========================================================================================================================*/
function WPP:DrawHint()
	if ( CurTime() > self.Hint_Time ) then
		return;
	end
	
	surface.SetFont("ChatFont");
	
	local W,H = surface.GetTextSize(WPP.Hint);
	local X , Y = (ScrW() / 2) - 5 - (W / 2), (ScrH() / 2) - 5 - (H /2);
	
	surface.SetDrawColor( 0 , 0, 0, 200 );
	surface.DrawRect(X,Y,W + 10,H + 10);
	
	surface.SetDrawColor( WPP.Hint_Color.r , WPP.Hint_Color.g , WPP.Hint_Color.b, 255 );
	surface.DrawOutlinedRect(X,Y,W + 10,H + 10);
	
	surface.SetTextColor( WPP.Hint_Color.r , WPP.Hint_Color.g , WPP.Hint_Color.b, 255 );
	surface.SetTextPos((ScrW() / 2) - (W / 2),(ScrH() / 2) - (H /2));
	surface.DrawText(WPP.Hint);
end

/*===========================================================================================================================		
	Usermessage: WPP-Hud
	Purpose: Recives a hint.
	Author: Goluch
===========================================================================================================================*/
usermessage.Hook("WPP-Hud",function(UM)
	WPP.Hint = UM:ReadString();
	WPP.Hint_Color = Color(UM:ReadChar(),UM:ReadChar(),UM:ReadChar());
	WPP.Hint_Time = CurTime() + UM:ReadChar();
end);




